package cn.edu.lzzy.services.Impl;

import ch.qos.logback.core.spi.AppenderAttachableImpl;
import cn.edu.lzzy.dao.ApplicantRepository;
import cn.edu.lzzy.dao.CourseRepository;
import cn.edu.lzzy.dao.UserRepository;
import cn.edu.lzzy.entities.Applicant;
import cn.edu.lzzy.entities.Course;
import cn.edu.lzzy.entities.User;
import cn.edu.lzzy.services.ApplicantService;
import cn.edu.lzzy.utils.AuthUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.transaction.Transactional;
import java.util.List;
import java.util.UUID;

public class ApplicantServiceImpl implements ApplicantService {
    private final UserRepository userRepository;
    private final CourseRepository courseRepository;
    private final ApplicantRepository applicantRepository;
    @Autowired
    public ApplicantServiceImpl(UserRepository userRepository, CourseRepository courseRepository, ApplicantRepository applicantRepository) {
        this.userRepository = userRepository;
        this.courseRepository = courseRepository;
        this.applicantRepository = applicantRepository;
    }

    @Transactional(rollbackOn=Exception.class)//
    @Override
    public Applicant apply(UUID studentId, UUID courseId, String token) {
        if(AuthUtils.invalid(studentId,token)){//非法用户
            return null;
        }
        User student=userRepository.findById(studentId).orElse(null);
        if(student==null){
            return null;
        }
        Course course=courseRepository.findById(studentId).orElse(null);
        if(student==null){
            return null;
        }
        Applicant applicant=new Applicant();
        applicant.setStudent(student);
        applicant.setCourse(course);
        applicant.setStatus(Applicant.STATUS_APPLY);//
        return applicantRepository.save(applicant);
    }
    @Transactional(rollbackOn=Exception.class)//
    @Override
    public List<Applicant> getAllCourses(UUID studentId, String token) {
        if(AuthUtils.invalid(studentId,token)){
            return null;
        }
        return applicantRepository.findApplicantsByStudentIdEqualsAnAndStatusOrderByApplyTimeDesc(studentId,Applicant.STATUS_APPLY);
    }
    @Transactional(rollbackOn=Exception.class)//
    @Override
    public List<Applicant> getApplyingCourses(UUID studentId, String token) {
        if(AuthUtils.invalid(studentId,token)){
            return null;
        }
        return applicantRepository.findApplicantsByStudentIdEqualsAnAndStatusOrderByApplyTimeDesc(studentId,Applicant.STATUS_PASS);

    }
    @Transactional(rollbackOn=Exception.class)//
    @Override
    public List<Applicant> getApprovedCourses(UUID studentId, String token) {
        if(AuthUtils.invalid(studentId,token)){
            return null;
        }
        return applicantRepository.findApplicantsByStudentIdEqualsAnAndStatusOrderByApplyTimeDesc(studentId,Applicant.STATUS_PASS);

    }
    @Transactional(rollbackOn=Exception.class)//
    @Override
    public List<Applicant> getDeclinedCourses(UUID studentId, String token) {
        if(AuthUtils.invalid(studentId,token)){
            return null;
        }
        return applicantRepository.findApplicantsByStudentIdEqualsAnAndStatusOrderByApplyTimeDesc(studentId,Applicant.STATUS_REFUSE);

    }
    private Applicant approvalOpt(UUID applicantId,String token,int statusVal){
        Applicant applicant=applicantRepository.findById(applicantId).orElse(null);
        if(applicant==null){
            return null;
    }
        //获取教师数据，验证其合法性
        Course course=applicant.getCourse();
        UUID teacherId=course.getTeacher().getId();
        if(AuthUtils.invalid(teacherId,token)){
            return null;
        }

        //审批状态设置为通过审批
        applicant.setStatus(statusVal);

        return applicantRepository.save(applicant);
    }





    @Transactional(rollbackOn=Exception.class)//
    @Override
    public Applicant approve(UUID applicantId, String token) {
      Applicant applicant=approvalOpt(applicantId,token,Applicant.STATUS_PASS);
        //构建返回的对象的信息，构建课程选修的学生数据，并完成持久化。
        if(applicant!=null){
            User student=applicant.getStudent();
            Course course=applicant.getCourse();//  获取课程信息
            List<User> students=course.getStudents();
            if(!students.contains(student)){
                students.add((student));
            }
            course.setStudents(students);//更新到新的学生列表里面

            applicant.setCourse(courseRepository.save(course));
        }
        return applicant;
    }
    @Transactional(rollbackOn=Exception.class)//
    @Override
    public Applicant decline(UUID applicantId, String token) {

        Applicant applicant=approvalOpt(applicantId,token,Applicant.STATUS_REFUSE);
        return applicant;
    }

    @Transactional(rollbackOn=Exception.class)//
    @Override
    public Course removeStudent(UUID applicantId, UUID studentId, String token) {
return null;
    }
    @Transactional(rollbackOn=Exception.class)//
    @Override
    public List<Applicant> getApplyingStudents(UUID courseId, String token) {
        return null;
    }
    @Transactional(rollbackOn=Exception.class)//
    @Override
    public List<Applicant> getApprovedStudents(UUID courseId, String token) {
        return null;
    }
    @Transactional(rollbackOn=Exception.class)//
    @Override
    public List<Applicant> getDeclineStudents(UUID courseId, String token) {
        return null;
    }
}
